package lk.mrt.cse.wihidum.trace;

import java.util.Map;
import java.util.HashMap;

public class TotalMessageTrace
{
    private static Map<String, String> traces = new HashMap<String, String>();

    public static void addMessageTrace(String trace) {

        System.out.println(trace);
        try {
            String id = trace.split("#")[0] + "#" + trace.split("#")[1];
            if (traces.containsKey(id)) {
                String t = traces.get(id);
                traces.remove(id);
                t = t + "," + trace.split("#")[4];
                traces.put(id, t);
            }
            else {
                traces.put(id, trace);
            }
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }

    public static String getMessageTrace() {
        String trace = "";

        for (String t : traces.values()) {
            if (!trace.isEmpty())
                trace = trace + "@" + t;
            else
                trace = t;
        }

//        System.out.println("Intermediate GetTrace response: " +trace);
//
//        trace = trace.replaceAll("1.1.1.1", "1");
//        trace = trace.replaceAll("1.1.1.2", "2");
//        trace = trace.replaceAll("1.1.2.1", "3");
//        trace = trace.replaceAll("1.1.2.2", "4");
//        trace = trace.replaceAll("1.2.1.1", "5");
        
        System.out.println("GetTrace response: " +trace);
        return trace;
    }
}
